<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class homeController extends Controller {
   
    
    public function __construct($model, $action) {
        parent::__construct($model, $action);
           }
    public function getImageDetail($id) {
        return $this->_imageModel->getImageById($id);
    }
    public function getNewProduct(){
        $content='';
        $listNewProduct = $this->_productModel->getNewProduct();
        $content .= "<div class='maincontent'><div class='new_product'>";
        $content .= "<div class='title_new_product'>";
        $content .= "SẢN PHẨM MỚI";
        $content .= "</div>";
        $content .= "<div class='list_new_product'>";
        if($listNewProduct!=false) {
            foreach ($listNewProduct as $item) {
                    $image = $this->getImageDetail($item['idProduct']);
                    $content .= "<div class='products_list'>";
                    $content .= "<a href='/product/detail/".$item['idProduct']."' class='link_new_name'>";
                    $content .= " <a href ='/product/detail/".$item['idProduct']."'><img src='".$image['link']."' class='img_new_product'/>";
                    $content .= "<div class='new_name'>";
                    $content .= $item['name'];
                    $content .= "</div>";
                    $content .= "</a>";
                    $content .= "</div>"; 
            }
        }
        $content .= "</div>"; // 
        $content .= "</div>"; // div new_product
        return $content;
    }
    
    public function getOldProduct(){
        $content='';
        $listOldProduct = $this->_productModel->getOldProduct();

        $content .= ' <div id="left_contain">
        <div class="title_hp">
            <span class="title_content">SẢN PHẦM GẦN HẾT HẠN</span>
        </div>'; 
        
        if($listOldProduct!=false) {
            foreach ($listOldProduct as $item) {
                    $image = $this->getImageDetail($item['idProduct']);
                    $content .=' <a href="/product/detail/'.$item['idProduct'].' class="link_product">
                <div class="body_hp">
                    <img class="img_product" src="'.$image['link'].'" alt="sp" />
                     <a href ="/product/detail/'.$item["idProduct"].'"><p class="title_product">'.$item['name'].'</p></a>
                    <p class="date_end_product"><img src="/public/images/Clock4.png"/>  Ngày kết thúc: '.$item['timeEnd'].'</p>
                    <div class="clear_float"></div>
                </div>
            </a>';
            }	
        
        }	
        $content .='</div>'; //div left_contain
        return $content;
    }
    
    public function getHotProduct(){
        $content='';
        $content .='  <div id="right_contain">
        <div class="title_hp">
            <span class="title_content">SẢN PHẨM HOT</span>
        </div>';
       
        $listHotProduct = $this->_productModel->getHotProduct();
        if($listHotProduct !=false){
            foreach ($listHotProduct as $item) {
                $image = $this->getImageDetail($item['idProduct']);

        $content .='    <a href="/product/detail/'.$item['idProduct'].'" class="link_product">
                <div class="body_hp">
                    <img class="img_product" src="'.$image['link'].'" alt="sp" />
                     <a href ="/product/detail/'.$item["idProduct"].'"><p class="title_product">'.$item['name'].'</p></a>
                    <p class="date_end_product"><img src="/public/images/Clock4.png"/> Ngày kết thúc:'.$item['timeEnd'].'</p>
                    <div class="clear_float"></div>
                </div>
            </a>';
            }
        }
        $content .='</div>'; //div right_contain
        return $content;
    }
    public function getUser()
    {
        
    }
    public function index() {
       $this->_memcached->delete('header');
       $this->_memcached->delete('homeProduct');
        // Not exist new_product in memcached server
       
        
        if(!$this->_memcached->get('homeProduct')) {
            $homecontent= $this->getNewProduct() . $this->getOldProduct() . $this->getHotProduct();
            $this->_memcached->set('homeProduct',$homecontent);
        }
        
        
        
        $HTMLContent = $this->getHeader().$this->_memcached->get('homeProduct');
        $HTMLContent .= '</div></div>'; //div wrapper_homepage
        $HTMLContent .= "</body>";
        $HTMLContent .= "</html>";   
        $this->_setView("index");
        $this->_view->set("content", $HTMLContent);
        return $this->_view->output();
    }
   
}

?>